// 分析相关API调用 - 与后端AnalyticsApi接口保持一致

import axios from 'axios'
import type { ProductBack, Sales, TimeSearch, UserStatsVO } from '@/types/analytics'

// 创建专门用于analytics的axios实例，不使用Session
const analyticsRequest = axios.create({
  baseURL: import.meta.env.DEV ? '' : 'http://192.168.110.12:8888',
  timeout: 10000,
  withCredentials: false // 禁用Cookie和Session传递
})

// analytics专用响应拦截器
analyticsRequest.interceptors.response.use(
  (response) => {
    const { data } = response
    // 后端统一返回格式：{code, success, msg, data}
    if (data.success) {
      return data
    } else {
      console.error('Analytics业务错误:', data.msg)
      return Promise.reject(new Error(data.msg))
    }
  },
  (error) => {
    console.error('Analytics网络错误:', error)
    return Promise.reject(error)
  }
)

// 销售关键指标
export const getSaleKeyMetrics = (timeSearch: TimeSearch) => {
  return analyticsRequest.post<Sales>('/api/analytics/sale-key-metrics', timeSearch)
}

// 支付方式统计
export const getSalePayType = (timeSearch: TimeSearch) => {
  return analyticsRequest.post<Array<{ payType: string, count: number }>>('/api/analytics/sale-pay-type', timeSearch)
}

// 商品关键指标
export const getProductKeyMetrics = (timeSearch: TimeSearch) => {
  return analyticsRequest.post<ProductBack>('/api/analytics/product-key-metrics', timeSearch)
}

// 商品库存统计
export const getProductStock = (timeSearch: TimeSearch) => {
  return analyticsRequest.post<Array<Record<string, any>>>('/api/analytics/product-stock', timeSearch)
}

// 商品分类统计
export const getProductCategory = (timeSearch: TimeSearch) => {
  return analyticsRequest.post<Array<Record<string, any>>>('/api/analytics/product-category', timeSearch)
}

// 用户状态统计
export const getUserStatus = () => {
  return analyticsRequest.get<UserStatsVO>('/api/analytics/user-status')
}

// 测试接口
export const testAnalytics = () => {
  return analyticsRequest.get<string>('/api/analytics/test')
} 